<template>
	<div class="reset_pwd col-sm-12" :style="{height: heightData}">
		<div class="sign_limit_width signform">
			<img src="../assets/img/logo.png" alt="" class="sign_logo cursor" @click="goHome()"/>
			<h2>重置密码</h2>
			<div class="form-group text-left">
				<label for="password">密码</label>
				<input type="password" class="form-control" placeholder="长度6-30位" v-model="resetPassword" @blur="passwInvalid()" v-bind:class="resetClassTip0"/></br>
			</div>
			<button class="btn btn_sign" @click="resetPwd()">确认修改</button> 
			<p class="log_tips" v-if="seen">{{resetTip}}</p>			
			<p class="test_val" style="opacity:0;">{{$route.params}}</p>	
			<p class="footer_company">© 2016-2018 北京晶派科技有限公司</p>
		</div>
	</div>	
</template>

<script>
	import {urlResetPwd} from '../api/api';
	export default {
		name: 'resetPwd',
		data () {
		    return {
                userName:localStorage.getItem('username'),
                token:localStorage.getItem('res_token'),
                seen:false,
                resetPassword:'',
                resetClassTip0:{
		    		'form-control':true,
			        'tip': false
		    	},
		    	resetTip:'',
		    	heightData:''
		    }
		},
		mounted(){
	    	this.heightData=`${document.documentElement.clientHeight}px`;
	    },
		methods: {	
			goHome(){
				this.$router.replace('/');
			},
			passwInvalid:function(){
				let passReg=/^[0-9a-zA-Z?!_-]{6,30}$/;	
				if(! passReg.test(this.resetPassword)){
					this.resetClassTip0.tip=true;
					this.seen=true;
		            this.resetTip='请输入6-30位密码'
				 	$(".btn_sign").attr("disabled",true)
				}else{
					this.seen=false;	
				 	this.resetClassTip0.tip=false;
				 	$(".btn_sign").attr("disabled",false)
				}         
	       	},	       
	       	resetPwd:function(){
	       		if(this.resetPassword =='' ){
	       			this.seen=true;	
					return false;
				}else{	
//					let md5 = require('md5');
					let str=$('.test_val').text(),
					    sendData = eval('(' + str + ')'),
					    addKey = "password";
					sendData[addKey] =this.resetPassword;
	        		//重置密码请求
	         		urlResetPwd(sendData).then(response => {
		              	if(response.data.code==200){
		              		localStorage.clear();
	            			this.$router.replace('/login');
		              	}else{
		              		this.seen=true;
		              		this.resetTip='重置密码失败'
		              	}
		            })
				}
	       	}
		}
	}
</script>

<style>
	@import "../assets/css/sign.css"
</style>